package com.codi.fcloud.ufx.hssale.query;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ExceptionUtil;
import com.codi.base.util.StringUtil;
import com.codi.bus.exception.HundsunAPIException;
import com.codi.fcloud.ufx.HsUtil;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.HSInputParameterNames;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.codi.trade.dto.HsDividendQryDto;
import com.codi.trade.response.HsFundDividendQryResponse;
import com.codi.trade.service.DividendsQueryService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("dividendsQueryService")
public class DividendsQuery extends UfxSDKFactory implements DividendsQueryService {

    // 份额查询
    @Loggable
    @Override
    public Map<String, Object> query(Integer requestNum, Integer queryFlag, Integer beginNum, String sortDirection,
                                     String tradeAccount, String taAccount, String clientId, String fundCode) {
        try {
            // 获取第一页
            Map<String, Object> request = getRequestParams(requestNum, queryFlag, beginNum, sortDirection,
                tradeAccount, taAccount, clientId, fundCode);

            List<Map<String, Object>> response = getUfxSDK().query(getTarget(), request);

            // 如果requestNum是我们系统定义的最大值，则表明数据需要全部取出来
            getNextResponse(request, response, requestNum);

            return appendLog(request, response);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }

    }

    @Loggable
    @Override
    public List<HsFundDividendQryResponse> query(HsDividendQryDto dto) {

        String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);
        Map<String, Object> request = new HashMap<>();

        request.put("trust_way", getString(dto.getTrust_way(), trustWay));//	C1	交易委托方式	N
        request.put("net_no", getString(dto.getNet_no(), DEF_NET_NO));//	C9	网点代码	N	不传则使用默认值"0001" ，有传值（包括空）则使用所传值
        request.put("request_num", getInteger(dto.getRequest_num(), DEF_MAX_REQUEST_NUM));//	N8	请求行数	Y
        request.put("reqry_recordsum_flag", getString(dto.getReqry_recordsum_flag()));//	C1	重新统计总记录数标志	N
        request.put("qry_beginrownum", getInteger(dto.getQry_beginrownum(), 1));//	N8	查询起始行号	N
        request.put("trade_acco", getString(dto.getTrade_acco()));//	C17	交易账号	N
        request.put("ta_acco", getString(dto.getTa_acco()));//	C12	TA账号	N
        request.put("capital_mode", getString(dto.getCapital_mode()));//	C1	资金方式	N
        request.put("client_id", getString(dto.getClient_id()));//	C17	客户编号	N
        request.put("fund_code", getString(dto.getFund_code()));//	C6	基金代码	N
        request.put("confirmdate_begin", getString(dto.getConfirmdate_begin()));//	C8	确认日期起始日期	N
        request.put("confirmdate_end", getString(dto.getConfirmdate_end()));//	C8	确认日期终止日期	N
        request.put("trade_source", getString(dto.getTrade_source()));//	C12	交易来源	N
        request.put("ta_serial_id", getString(dto.getTa_serial_id()));//	C20	TA确认编号	N
        request.put("sort_direction", getString(dto.getSort_direction()));//	C1	排序方式	N
        request.put("private_flag", getString(dto.getPrivate_flag()));//	C1	是否为私募基金	N	0-否；1-是

        try {
            List<Map<String, Object>> response = getUfxSDK().query(getTarget(), request);

            return HsUtil.convertList(appendLog(request, response), HsFundDividendQryResponse.class);
        } catch (Exception e) {
            logger.error("fail to query dividend list, plz check");
        }
        return null;
    }

    @Loggable
    @Override
    public Map<String, Object> queryDto(HsDividendQryDto dto) {

        String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);
        Map<String, Object> request = new HashMap<>();

        request.put("trust_way", getString(dto.getTrust_way(), trustWay));//	C1	交易委托方式	N
        request.put("net_no", getString(dto.getNet_no(), DEF_NET_NO));//	C9	网点代码	N	不传则使用默认值"0001" ，有传值（包括空）则使用所传值
        request.put("request_num", getInteger(dto.getRequest_num(), DEF_MAX_REQUEST_NUM));//	N8	请求行数	Y
        request.put("reqry_recordsum_flag", getString(dto.getReqry_recordsum_flag()));//	C1	重新统计总记录数标志	N
        request.put("qry_beginrownum", getInteger(dto.getQry_beginrownum(), 1));//	N8	查询起始行号	N
        request.put("trade_acco", getString(dto.getTrade_acco()));//	C17	交易账号	N
        request.put("ta_acco", getString(dto.getTa_acco()));//	C12	TA账号	N
        request.put("client_id", getString(dto.getClient_id()));//	C17	客户编号	N
        request.put("capital_mode", getString(dto.getCapital_mode()));//	C1	资金方式	N
        request.put("fund_code", getString(dto.getFund_code()));//	C6	基金代码	N
        request.put("confirmdate_end", getString(dto.getConfirmdate_end()));//	C8	确认日期终止日期	N
        request.put("confirmdate_begin", getString(dto.getConfirmdate_begin()));//	C8	确认日期起始日期	N
        request.put("trade_source", getString(dto.getTrade_source()));//	C12	交易来源	N
        request.put("ta_serial_id", getString(dto.getTa_serial_id()));//	C20	TA确认编号	N
        request.put("sort_direction", getString(dto.getSort_direction()));//	C1	排序方式	N
        request.put("private_flag", getString(dto.getPrivate_flag()));//	C1	是否为私募基金	N	0-否；1-是

        try {
            List<Map<String, Object>> response = getUfxSDK().query(getTarget(), request);

            // 如果requestNum是我们系统定义的最大值，则表明数据需要全部取出来
            getNextResponse(request, response, dto.getRequest_num());
            return appendLog(request, response);

        } catch (Exception e) {
            logger.error("fail to query dividend list, plz check");
        }
        return null;
    }

    /**
     * 获取请求参数
     *
     * @param requestNum
     * @param queryFlag
     * @param beginNum
     * @param sortDirection
     * @param fundCode
     * @return
     */
    private Map<String, Object> getRequestParams(Integer requestNum, Integer queryFlag, Integer beginNum,
                                                 String sortDirection, String tradeAccount, String taAccount, String clientId, String fundCode) {
        String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);
        Map<String, Object> request = new HashMap<String, Object>();
        request.put("trust_way", trustWay); // 交易委托方式
        request.put("request_num", requestNum); // 请求行数
        request.put("reqry_recordsum_flag", queryFlag); // 重新统计总记录数标志
        request.put("qry_beginrownum", beginNum); // 查询起始行号
        request.put("sort_direction", sortDirection); // 返回排序方式
        request.put("trade_acco", tradeAccount); // 交易帐号
        request.put("ta_acco", taAccount); // 交易帐号
        request.put("client_id", clientId); // 客户编号
        request.put("fund_code", StringUtil.isEmpty(fundCode) ? "" : fundCode); // 基金代码

        return request;
    }

    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.DIVI_QRY;
    }

}
